package com.zrwl.poscloud.xo.service.impl;
import com.zrwl.poscloud.base.entity.CallProductOrder;
import com.zrwl.poscloud.base.entity.CallProductOrderHandle;
import com.zrwl.poscloud.base.entity.UserServiceOrderHandle;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.zrwl.poscloud.base.enums.EStatus;
import com.zrwl.poscloud.base.superI.SuperServiceImpl;
import com.zrwl.poscloud.commons.utils.ResultUtil;
import com.zrwl.poscloud.commons.utils.StringUtils;
import com.zrwl.poscloud.xo.global.MessageConf;
import com.zrwl.poscloud.xo.global.SQLConf;
import com.zrwl.poscloud.xo.global.SysConf;
import com.zrwl.poscloud.xo.mapper.CallProductOrderMapper;
import com.zrwl.poscloud.xo.service.CallProductOrderHandleService;
import com.zrwl.poscloud.xo.vo.CallProductOrderHandleVO;
import com.zrwl.poscloud.xo.vo.CallProductOrderVO;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;
import com.zrwl.poscloud.xo.mapper.UserServiceOrderHandleMapper;
import com.zrwl.poscloud.xo.service.UserServiceOrderHandleService;
import com.zrwl.poscloud.xo.vo.UserServiceOrderHandleVO;

import javax.annotation.Resource;

/**
 * 服务订单处理 服务实现类
 *
 * @author 刘思童 AutoGenerate
 * @date 2023-05-23
 */
@Service
public class UserServiceOrderHandleServiceImpl extends SuperServiceImpl<UserServiceOrderHandleMapper,UserServiceOrderHandle> implements UserServiceOrderHandleService {

    @Autowired
    private UserServiceOrderHandleService userServiceOrderHandleService;
    @Resource
    private UserServiceOrderHandleMapper userServiceOrderHandleMapper;
    @Autowired
    private CallProductOrderHandleService callProductOrderHandleService;
    @Resource
    private CallProductOrderMapper callProductOrderMapper;
    @Override
    public IPage<UserServiceOrderHandleVO> getPageList(UserServiceOrderHandleVO userServiceOrderHandleVO){

       Page<UserServiceOrderHandleVO> page = new Page<>();
       page.setCurrent(userServiceOrderHandleVO.getCurrentPage());
       page.setSize(userServiceOrderHandleVO.getPageSize());
       IPage<UserServiceOrderHandleVO> pageList = userServiceOrderHandleMapper.getOrderPageList(page, userServiceOrderHandleVO);
       List<UserServiceOrderHandleVO> list = pageList.getRecords();
       pageList.setRecords(list);
       return pageList;
}
    @Override
    public List<UserServiceOrderHandle> getAllList(UserServiceOrderHandleVO userServiceOrderHandleVO){
        QueryWrapper<UserServiceOrderHandle> queryWrapper = new QueryWrapper<>();
        queryWrapper.ne(SQLConf.STATUS, EStatus.DISABLED);
        queryWrapper.orderByAsc(SQLConf.CREATE_TIME);
        List<UserServiceOrderHandle> list = userServiceOrderHandleService.list(queryWrapper);
        return list;
}
    @Override
    public String add(UserServiceOrderHandleVO userServiceOrderHandleVO){
        UserServiceOrderHandle entity = new UserServiceOrderHandle();
        BeanUtils.copyProperties(userServiceOrderHandleVO, entity, SysConf.STATUS);
        entity.insert();
        userServiceOrderHandleVO.setUid(entity.getUid());
        CallProductOrderVO callProductOrderVO=callProductOrderMapper.getCallProductOrder(userServiceOrderHandleVO.getOrderUid());
        if (callProductOrderVO !=null) {
            CallProductOrderHandleVO callProductOrderHandleVO = new CallProductOrderHandleVO();
            callProductOrderHandleVO.setOrderUid(callProductOrderVO.getUid());
            callProductOrderHandleVO.setAdminUid(userServiceOrderHandleVO.getAdminUid());
            callProductOrderHandleVO.setEmployeeUid(userServiceOrderHandleVO.getEmployeeUid());
            callProductOrderHandleVO.setUserUid(userServiceOrderHandleVO.getUserUid());
            callProductOrderHandleVO.setSummary(userServiceOrderHandleVO.getSummary());
            callProductOrderHandleVO.setRefundAmt(callProductOrderVO.getAmt());
            callProductOrderHandleVO.setCode(callProductOrderVO.getCode());
            callProductOrderHandleVO.setType(userServiceOrderHandleVO.getType());
            callProductOrderHandleVO.setRefundStatus(userServiceOrderHandleVO.getRefundStatus());
            callProductOrderHandleVO.setStatus(userServiceOrderHandleVO.getStatus());
            callProductOrderHandleVO.setErrorMsg(userServiceOrderHandleVO.getErrorMsg());
            callProductOrderHandleVO.setUid(callProductOrderHandleVO.getUid());
            callProductOrderHandleService.add(callProductOrderHandleVO);
            return ResultUtil.successWithMessage(MessageConf.INSERT_SUCCESS);
        }else {
            return ResultUtil.errorWithMessage("该订单已退款");
        }
    }
    @Override
    public String edit(UserServiceOrderHandleVO userServiceOrderHandleVO){
        UserServiceOrderHandle entity = userServiceOrderHandleService.getById(userServiceOrderHandleVO.getUid());
        BeanUtils.copyProperties(userServiceOrderHandleVO, entity);
        entity.updateById();
        return ResultUtil.successWithMessage(MessageConf.UPDATE_SUCCESS);
}
    @Override
    public String delete(UserServiceOrderHandleVO userServiceOrderHandleVO){
        UserServiceOrderHandle entity = userServiceOrderHandleService.getById(userServiceOrderHandleVO.getUid());
        entity.setStatus(EStatus.DISABLED);
        entity.updateById();
        return ResultUtil.successWithMessage(MessageConf.UPDATE_SUCCESS);
}

    @Override
    public IPage<UserServiceOrderHandleVO> getRefundOrderList(UserServiceOrderHandleVO userServiceOrderHandleVO) {
        Page<UserServiceOrderHandleVO> page = new Page<>();
        page.setCurrent(userServiceOrderHandleVO.getCurrentPage());
        page.setSize(userServiceOrderHandleVO.getPageSize());
        IPage<UserServiceOrderHandleVO> pageList = userServiceOrderHandleMapper.getRefundOrderList(page, userServiceOrderHandleVO);
        List<UserServiceOrderHandleVO> list = pageList.getRecords();
        pageList.setRecords(list);
        return pageList;
    }

    @Override
    public IPage<UserServiceOrderHandleVO> getOrderListByDay(UserServiceOrderHandleVO userServiceOrderHandleVO) {
        Page<UserServiceOrderHandleVO> page = new Page<>();
        page.setCurrent(userServiceOrderHandleVO.getCurrentPage());
        page.setSize(userServiceOrderHandleVO.getPageSize());
        IPage<UserServiceOrderHandleVO> pageList = userServiceOrderHandleMapper.getOrderListByDay(page, userServiceOrderHandleVO);
        List<UserServiceOrderHandleVO> list = pageList.getRecords();
        pageList.setRecords(list);
        return pageList;
    }
}
